package com.hjx.dao.Impl;

import com.hjx.dao.jojjle.orderDao;
import com.hjx.pojo.Order;
import com.hjx.pojo.User;
import com.hjx.util.JdbcUtil;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CheckedOutputStream;

public class orderImpl implements orderDao {
    private  ResultSet resultSet=null;
    private  String product_name=null;
    private  String supplier=null;
    private String payStatus=null;

//    根据
    @Override
    public List<Order> getAlltOrder() throws Exception{
        List<Order> orderList=new ArrayList<>();



        String sql="select* from order";


        //执行操作前，先创建连接对象
        JdbcUtil.createConnection();
        resultSet= JdbcUtil.find(null,sql);

        while(resultSet.next()){
            Order order=new Order();
            order.setId(resultSet.getInt("id"));
            order.setCoding(resultSet.getString("coding"));
            order.setProduct_name(resultSet.getString("product_name"));
            order.setSupplier(resultSet.getString("supplier"));
            order.setOrder_money(resultSet.getString("order_money"));
            order.setPayStatus(resultSet.getString("payStatus"));
            order.setCreateTime(resultSet.getString("createTime"));
            //将查询出来的user对象存入list集合中
            orderList.add(order);
        }
//        关闭资源
        JdbcUtil.closeResources(resultSet);
        return orderList;
    }

//    查询指定条件的订单
    public List<Order> getOrder(Object[] args)throws Exception{
        product_name= (String) args[0];
        supplier= (String) args[1];
        payStatus= (String) args[2];
        if(product_name==null ){
            product_name="";
        }
        if (supplier==null){
            supplier="";
        }
        if(payStatus==null){
            payStatus="";
        }



        List<Order> orderList=new ArrayList<>();
//        String sql="select* from `order` where product_name like ? or supplier like ? and payStatus like ?";
        String sql="select* from `order` where product_name like"+" '%"+args[0]+"%' or"+" supplier like"+" '%"+args[1]+"%' or"+" payStatus like"+" '%"+args[2]+"%'";
        System.out.println("查询sql：");
        System.out.println(sql);
        //执行操作前，先创建连接对象
        JdbcUtil.createConnection();
        System.out.println("args=");
        System.out.println(args);

        resultSet= JdbcUtil.find(null,sql);
        if(resultSet!=null){
            System.out.println("查询到order了");
            while(resultSet.next()){
                Order order=new Order();
                order.setId(resultSet.getInt("id"));
                order.setCoding(resultSet.getString("coding"));
                order.setProduct_name(resultSet.getString("product_name"));
                order.setSupplier(resultSet.getString("supplier"));
                order.setOrder_money(resultSet.getString("order_money"));
                order.setPayStatus(resultSet.getString("payStatus"));
                order.setCreateTime(resultSet.getString("createTime"));
                //将查询出来的user对象存入list集合中
                orderList.add(order);
            }
        }else{
            System.out.println("查询不到order数据");
        }

//        关闭资源
        JdbcUtil.closeResources(resultSet);
        return orderList;
}

    @Override
    public int updateOrder(String[] args) throws Exception {
//        sql语句没有注入进去，换成下面那条语句可以修改成功   更新成功
        String sql="update `order` set product_name="+"'"+args[0]+"'"+",supplier="+"'"+args[1]+"'"+",order_money="+"'"+args[2]+"'"+",payStatus="+"'"+args[3]+"'"+" where coding="+"'"+args[4]+"'";
        System.out.println("sql="+sql);
        JdbcUtil.createConnection();
        int count=JdbcUtil.Change(null,sql);
        if (count>0){
            System.out.println("success");
        }else {
            System.out.println("update fail");
        }
        JdbcUtil.closeResources(null);
        return count;
    }

    @Override
    public int delOrder(Object[] args) throws Exception {
        int count=0;
        String sql="delete from `order` where coding="+"'"+args[0]+"'";
        JdbcUtil.createConnection();
        count=JdbcUtil.Change(null, sql);
        System.out.println("处理删除---count="+count);
        JdbcUtil.closeResources(null);
        return count;
    }

    @Override
    public int addOrder(String[] args) throws Exception {
        int count=0;
        String sql="insert into `order`(coding,product_name,supplier,order_money,payStatus,createTime) " +
                "values("+"'"+args[0]+"'"+","+"'"+args[1]+"'"+","+"'"+args[2]+"'"+","+"'"+args[3]+"'"+","+"'"+args[4]+"'"+","+"'"+args[5]+"'"+ ")";
        JdbcUtil.createConnection();
        count=JdbcUtil.Change(null,sql);
        JdbcUtil.closeResources(null);
        return count;
    }


}
